package com.puhui.lib.widgets.indicator

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.ToastUtils
import com.puhui.lib.BuildConfig
import com.puhui.lib.R

class MainTabIndicator @JvmOverloads constructor(
    context: Context,
    attributes: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attributes, defStyleAttr) {

    private var mContext: Context = context
    private var tabClickListener: TabClickListener? = null
    private var lastSelectedTabIndex: Int = 0 //上次选中状态的tab

    private val tabImgList: MutableList<ImageView> = ArrayList(5)
    private val tabTvList: MutableList<TextView> = ArrayList(5)
    private val tabLayoutList: MutableList<View> = ArrayList(5)
    private val tabBadgeList: MutableList<TextView> = ArrayList(5)

    private val tabObjList: MutableList<TabObject> = ArrayList(5)

    init {
        val view = LayoutInflater.from(mContext).inflate(R.layout.main_tab_indicator_layout, this, false)
        addView(view)

        initView(view)
        initListener()
    }

    /**
     * 切换tab页面
     */
    fun changeCustomTab(tabClassName: String) {
        for (index in tabObjList.indices) {
            if (tabObjList[index].tabClassName == tabClassName) {
                doChangeTab(index)
                return
            }
        }
    }

    /**
     * 设置角标
     */
    fun setTabBadgeText(tabClassName: String, badgeValue: String) {
        for (index in tabObjList.indices) {
            if (tabObjList[index].tabClassName == tabClassName) {
                tabBadgeList[index].text = badgeValue
                if (badgeValue.isEmpty()) tabBadgeList[index].visibility = GONE
                return
            }
        }
    }

    private fun initListener() {
        for (index in tabLayoutList.indices) {
            tabLayoutList[index].tag = index
            tabLayoutList[index].setOnClickListener { v ->
                val position = v.tag as Int

                doChangeTab(position)
            }
        }
    }

    private fun doChangeTab(position: Int) {
        if (!NetworkUtils.isConnected()) {
            if (BuildConfig.DEBUG) ToastUtils.showShort("Network not connected")
            return
        }

        tabClickListener?.let {
            if (lastSelectedTabIndex == position) {
                it.onSelectedTabClick(tabObjList[position])
            } else {
                val canChange = it.onTabSelected(tabObjList[position])
                if (!canChange) return@doChangeTab

                tabImgList[lastSelectedTabIndex].isSelected = false
                tabTvList[lastSelectedTabIndex].isSelected = false
                var imageView = tabImgList[lastSelectedTabIndex]
                imageView.animate().setDuration(100).scaleX(1.0f).scaleY(1.0f).start()

                lastSelectedTabIndex = position
                tabImgList[position].isSelected = true
                tabTvList[position].isSelected = true
                imageView = tabImgList[position]
                imageView.animate().setDuration(100).scaleX(1.1f).scaleY(1.1f).start()
            }
        }
    }

    fun setOnTabClickListener(listener: TabClickListener) {
        tabClickListener = listener
    }

    fun initData(tabObjects: MutableList<TabObject>) {
        tabObjList.addAll(tabObjects)

        lastSelectedTabIndex = 0
        for (index in tabObjList.indices) {
            val tabObject = tabObjList[index]
            tabImgList[index].setImageResource(tabObject.tabIconRes)
            tabImgList[index].isSelected = index == lastSelectedTabIndex

            tabTvList[index].text = context.getString(tabObject.tabNameId)
            tabTvList[index].isSelected = index == lastSelectedTabIndex
        }
        tabImgList[lastSelectedTabIndex].scaleX = 1.2f
        tabImgList[lastSelectedTabIndex].scaleY = 1.2f
    }

//    fun initData(@ArrayRes tabIconResIdName: Int, @ArrayRes tabClassName: Int, @ArrayRes tabName: Int) {
//        val tabIconNames = context.resources?.getStringArray(tabIconResIdName)
//        val tabClassNames = context.resources?.getStringArray(tabClassName)
//        val tabNames = context.resources?.getStringArray(tabName)
//
//        if (tabIconNames == null || tabClassNames == null || tabNames == null) {
//            return
//        }
//        if (tabIconNames.size != tabClassNames.size && tabIconNames.size != tabNames.size) {
//            return
//        }
//
//        for (index in tabClassNames.indices) {
//            val drawableId = ResourceUtils.getDrawableIdByName(tabIconNames[index])
//            tabObjList.add(TabObject(tabNames[index], drawableId, tabClassNames[index]))
//        }
//
//        lastSelectedTabIndex = 0
//        for (index in tabObjList.indices) {
//            val tabObject = tabObjList[index]
//            tabImgList[index].setImageResource(tabObject.tabIconRes)
//            tabImgList[index].isSelected = index == lastSelectedTabIndex
//
//            tabTvList[index].text = tabObject.tabName
//            tabTvList[index].isSelected = index == lastSelectedTabIndex
//        }
//        tabImgList[lastSelectedTabIndex].scaleX = 1.2f
//        tabImgList[lastSelectedTabIndex].scaleY = 1.2f
//    }

    private fun initView(view: View) {
        tabLayoutList.add(view.findViewById(R.id.firstTabLayout))
        tabLayoutList.add(view.findViewById(R.id.secondTabLayout))
        tabLayoutList.add(view.findViewById(R.id.thirdTabLayout))
        tabLayoutList.add(view.findViewById(R.id.fourthTabLayout))
        tabLayoutList.add(view.findViewById(R.id.fifthTabLayout))

        tabImgList.add(view.findViewById(R.id.firstTabImg))
        tabImgList.add(view.findViewById(R.id.secondTabImg))
        tabImgList.add(view.findViewById(R.id.thirdTabImg))
        tabImgList.add(view.findViewById(R.id.fourthTabImg))
        tabImgList.add(view.findViewById(R.id.fifthTabImg))

        tabTvList.add(view.findViewById(R.id.firstTabTv))
        tabTvList.add(view.findViewById(R.id.secondTabTv))
        tabTvList.add(view.findViewById(R.id.thirdTabTv))
        tabTvList.add(view.findViewById(R.id.fourthTabTv))
        tabTvList.add(view.findViewById(R.id.fifthTabTv))

        tabBadgeList.add(view.findViewById(R.id.badgeFirstTv))
        tabBadgeList.add(view.findViewById(R.id.badgeSecondTv))
        tabBadgeList.add(view.findViewById(R.id.badgeThirdTv))
        tabBadgeList.add(view.findViewById(R.id.badgeFourTv))
        tabBadgeList.add(view.findViewById(R.id.badgeFiveTv))
    }

    interface TabClickListener {
        /**
         * 点击了未选中状态的tab
         */
        fun onTabSelected(tabObj: TabObject): Boolean = true

        /**
         * 点击已未选中状态的tab
         */
        fun onSelectedTabClick(tabObj: TabObject) {}

        fun onTabUnSelected() {}
    }
}

class TabObject constructor(
    @StringRes val tabNameId: Int,
    @DrawableRes val tabIconRes: Int,
    val tabClassName: String,
    var badgeValue: String = ""
)